﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace MyToolStripMenuItem
{
    public partial class MyToolStripMenuItem : ToolStripMenuItem
    {
        public enum State
        {
            normal,
            focused,
            mousedown
        }

        protected State state = State.normal;
        [Browsable(false)]
        public State ControlState
        {
            get
            {
                return state;
            }
            set
            {
                state = value;
            }
        }
        

        protected Image normalImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"HtControl.Res.blue.png"));
        [Category("自定义"), Description("正常状态的背景图片")]
        [Browsable(true)]
        public Image NormalImage
        {
            get
            {
                return normalImage;
            }
            set
            {
                normalImage = value;
                BackgroundImage = value;
            }
        }

        protected Image fousedImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"HtControl.Res.red.png"));
        [Category("自定义"), Description("高亮状态的背景图片")]
        [Browsable(true)]
        public Image FousedImage
        {
            get
            {
                return fousedImage;
            }
            set
            {
                fousedImage = value;
            }
        }

        protected Image mouseDownImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"HtControl.Res.green.png"));
        [Category("自定义"), Description("获取焦点状态的背景图片")]
        [Browsable(true)]
        public Image MouseDownImage
        {
            get
            {
                return mouseDownImage;
            }
            set
            {
                mouseDownImage = value;
            }
        }

        public MyToolStripMenuItem()
        {
            InitializeComponent();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            state = State.focused;
            base.OnMouseEnter(e);
            Invalidate();
            Console.WriteLine("OnMouseEnter in HtToolStripMenuItem");
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            state = State.normal;
            base.OnMouseLeave(e);
            Invalidate();
            Console.WriteLine("OnMouseLeave in HtToolStripMenuItem");
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            state = State.mousedown;
            base.OnMouseDown(e);
            Invalidate();
            Console.WriteLine("OnMouseDown in HtToolStripMenuItem");
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (ContentRectangle.Contains(e.X, e.Y))
            {
                state = State.focused;
            }
            else
            {
                state = State.normal;
            }
            base.OnMouseUp(e);
            Invalidate();
            Console.WriteLine("OnMouseUp in HtToolStripMenuItem");
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Graphics g = e.Graphics;
            int x = 0;
            int y = 0;
            Size s = g.MeasureString(Text, Font).ToSize();
            x = (Width - s.Width) / 2 + 15;
            y = (Height - s.Height) / 2;

            switch (state)
            {
                case State.normal:
                    g.DrawImage(normalImage, new Rectangle(0, 0, 30, 30));
                    g.DrawString(this.Text,this.Font,new SolidBrush(Color.White),new Point(x,y));
                    break;
                case State.focused:
                    g.DrawImage(fousedImage, new Rectangle(0, 0, 30, 30));
                    g.DrawString(this.Text, this.Font, new SolidBrush(Color.White), new Point(x,y));
                    break;
                case State.mousedown:
                    g.DrawImage(mouseDownImage, new Rectangle(0, 0, 30, 30));
                    g.DrawString(this.Text, this.Font, new SolidBrush(Color.White), new Point(x,y));
                    break;
            }
        }
    }
}
